package com.weixin.servlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weixin.util.GsonUtil;

/**
 * Servlet implementation class ReceiveServlet
 */
public class FirstServlet extends HttpServlet {
	Logger logger = Logger.getLogger(FirstServlet.class);
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FirstServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(GsonUtil.toJson(request.getParameterMap()));
			
		System.out.println("*****************get**************");
		System.out.println(request.getSession().getAttribute("py"));
		if(request.getSession().getAttribute("py") ==null){
			request.getSession().setAttribute("py", "just for test");
		}
		
		Map<String, String[]> map = request.getParameterMap();
		
		if(map !=null){
			for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				System.out.println(String.format("key:%s,value:%s", key,Arrays.toString(map.get(key))));
			}
		}
		
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		response.getWriter().write("<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"no\" ?>\n" +
				"<ANS VER=\"1.0\">\n" +
				"	<LCSCLIENTID>02100087</LCSCLIENTID>\n" +
				"	<ORID>13052452724</ORID>\n" +
				"	<LIA>\n" +
				"		<RESULT>0</RESULT>\n" +
				"		<POSINFOS>\n" +
				"			<POSINFO>\n" +
				"				<POSITIONRESULT>0</POSITIONRESULT>\n" +
				"				<MSID>14549254883</MSID>\n" +
				"				<MSID_TYPE>0</MSID_TYPE>\n" +
				"				<AREACODE>21</AREACODE>\n" +
				"				<LOCALTIME>20160519091913</LOCALTIME>\n" +
				"				<LATITUDETYPE>1</LATITUDETYPE>\n" +
				"				<LATITUDE>"+new Random().nextInt(500)+"</LATITUDE>\n" +
				"				<LONGITUDETYPE>0</LONGITUDETYPE>\n" +
				"				<LONGITUDE>1130136.874</LONGITUDE>\n" +
				"				<RADIUS>0</RADIUS>\n" +
				"				<POSOUR>6</POSOUR>\n" +
				"				<ROAMCITY>731</ROAMCITY>\n" +
				"			</POSINFO>\n" +
				"		</POSINFOS>\n" +
				"	</LIA>\n" +
				"</ANS>");
		System.out.println("*****************get**************");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		System.out.println("*****************post**************");
		
		Map<String, String[]> map = request.getParameterMap();
		
		if(map !=null){
			for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				System.out.println(String.format("key:%s,value:%s", key,Arrays.toString(map.get(key))));
			}
		}
		try {
			Thread.sleep(3500);
			
			response.getWriter().write("<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"no\" ?>\n" +
"<ANS VER=\"1.0\">\n" +
"	<LCSCLIENTID>02100087</LCSCLIENTID>\n" +
"	<ORID>13052452724</ORID>\n" +
"	<LIA>\n" +
"		<RESULT>0</RESULT>\n" +
"		<POSINFOS>\n" +
"			<POSINFO>\n" +
"				<POSITIONRESULT>0</POSITIONRESULT>\n" +
"				<MSID>14549254883</MSID>\n" +
"				<MSID_TYPE>0</MSID_TYPE>\n" +
"				<AREACODE>21</AREACODE>\n" +
"				<LOCALTIME>20160519091913</LOCALTIME>\n" +
"				<LATITUDETYPE>1</LATITUDETYPE>\n" +
"				<LATITUDE>280956.208</LATITUDE>\n" +
"				<LONGITUDETYPE>0</LONGITUDETYPE>\n" +
"				<LONGITUDE>1130136.874</LONGITUDE>\n" +
"				<RADIUS>0</RADIUS>\n" +
"				<POSOUR>6</POSOUR>\n" +
"				<ROAMCITY>731</ROAMCITY>\n" +
"			</POSINFO>\n" +
"		</POSINFOS>\n" +
"	</LIA>\n" +
"</ANS>");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("*****************post**************");
	}

}
